<template>
  <div class="container">
    <div class="chart">
      <WChart :option="option" width="100%" height="100%" />
    </div>
  </div>
</template>
<script>
import WChart from '@comp/chart/index.vue'
import * as echarts from 'echarts'
import website from '@/api/website'
export default {
  name: 'LeftItem2',
  components: {
    WChart
  },
  data() {
    return {
      option: {
        grid: {
          top: '40',
          left: '40',
          right: '10',
          bottom: '30'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          formatter: function (params) {
            var tar = params[1]
            return tar.name + ' : ' + tar.data
          }
        },
        xAxis: {
          type: 'category',
          data: ['公司级', '部门级', '班组级']
        },
        yAxis: {
          type: 'value',
          name: '单位：个'
        },
        series: [
          {
            type: 'bar',
            barWidth: 20,
            label: {
              show: true,
              position: 'top'
            },
            showBackground: true,
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: '#83bff6' },
                { offset: 0.5, color: '#188df0' },
                { offset: 1, color: '#188df0' }
              ])
            },
            emphasis: {
              itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: '#2378f7' },
                  { offset: 0.7, color: '#2378f7' },
                  { offset: 1, color: '#83bff6' }
                ])
              }
            },
            data: [65, 87, 62]
          }
        ]
      }
    }
  },
  mounted() {
    this.getApiData()
  },
  methods: {
    getApiData() {
      website.leftapi2().then((res) => {
        if (res.code === 'success') {
          if (res.result) {
            let data = res.result
            this.option.xAxis.data = data.map((item) => item.name)
            this.option.series[0].data = data.map((item) => item.value)
            console.log(this.option)
          }
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.container {
  display: flex;
  height: 100%;
  justify-content: right;
  .chart {
    width: 100%;
    height: 100%;
  }
  .title {
    width: 50%;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    align-items: center;
    justify-content: center;
    span {
      font-size: 16px;
      padding: 5px 10px;
    }
  }
}
</style>
